package chat.network.http;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import inter.Delegate;

/**
 * 加入房间
 * Created by 凯 on 2015/10/23.
 */
public class JoinRoom {
    Delegate delegate;
    UserJoinRoom userJoinRoom;

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void setUserJoinRoom(UserJoinRoom userJoinRoom) {
        this.userJoinRoom = userJoinRoom;
    }

    public JoinRoom() {
    }

    //
    public void join()
    {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url ="http://"+IP+"/Chat/JoinRoom";
        StringRequest stringRequest = new StringRequest(Request.Method.POST,url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (delegate != null)
                            delegate.onResponse(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                Gson gson = new Gson();
                map.put("data",gson.toJson(userJoinRoom));
                return  map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data,"utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }


    public static class UserJoinRoom {
        private Long user_ID;
        private Long room_ID;
        private String join_Name;
        private String join_Role;
        private String join_Time;

        public Long getUser_ID() {
            return user_ID;
        }
        public void setUser_ID(Long user_ID) {
            this.user_ID = user_ID;
        }
        public Long getRoom_ID() {
            return room_ID;
        }
        public void setRoom_ID(Long room_ID) {
            this.room_ID = room_ID;
        }
        public String getJoin_Name() {
            return join_Name;
        }
        public void setJoin_Name(String join_Name) {
            this.join_Name = join_Name;
        }
        public String getJoin_Role() {
            return join_Role;
        }
        public void setJoin_Role(String join_Role) {
            this.join_Role = join_Role;
        }
        public String getJoin_Time() {
            return join_Time;
        }
        public void setJoin_Time(String join_Time) {
            this.join_Time = join_Time;
        }
    }

    public static class JoinRoomStatus {
        public  static final String FAILED = "0";
        public static final String SUCCESS = "1";
        public static final String JOINED = "2";
        public static final String NOVALUE = "3";
    }

    public static class ResponseJoinRoom {
        private String status;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }
    }

}
